package fileupdownYouhua.dao;


import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class DatabaseDaoIml implements DatabaseDao{
    @Override
    public List<List> serach(boolean search,String filename) {
        List<List> listList = new ArrayList<>();
        List<String> list = new ArrayList<>();
        List<String> update = new ArrayList<>();
        List<String> id = new ArrayList<>();
        try {
            Connection connection = JdcbConnection.requestConnection();
            String sql ;
            if (search){
                sql = "select attachment_name,upload_date,file_id from file_table";
            }else {
                sql = "select attachment_name,upload_date,file_id from file_table where attachment_name= '"+filename+"'";
            }
            Statement statement = connection.createStatement();
            ResultSet resultSet = statement.executeQuery(sql);
            while (resultSet.next()){
                list.add(resultSet.getString("attachment_name"));
                update.add(resultSet.getString("upload_date"));
                id.add(resultSet.getString("file_id"));
            }
            listList.add(list);
            listList.add(update);
            listList.add(id);

        }catch (Exception e){
            e.printStackTrace();
        }
        return listList;
    }

    @Override
    public void addBase(String submittedFileName, String fileId, String url) {
        try {
            Connection connection = JdcbConnection.requestConnection();
            String sql = "INSERT INTO file_table (file_id,attachment_name, url, real_name) " +
                    "VALUES (?,?,?,?) ";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,fileId);
            preparedStatement.setString(2,submittedFileName);
            preparedStatement.setString(3,url);
            preparedStatement.setString(4,submittedFileName);
            preparedStatement.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void deleteBase(String shan, String name) {
        try {
            Connection connection = JdcbConnection.requestConnection();
            Statement statement = connection.createStatement();
            String sql = "DELETE FROM file_table WHERE file_id = '"+shan+"'";
            int i = statement.executeUpdate(sql);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }


}
